package com.dai.chun.dao.action;

import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.dai.chun.dao.dao.DepartmentDao;
import com.dai.chun.dao.impl.DepartmentDaoImpl;
import com.dai.chun.dao.model.Department;
import com.dai.chun.util.PageSplitUtil;

public class DepartmentAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private DepartmentDao departmentDao;
	private Department department;
	private List<Department> departmentList;
	private PageSplitUtil psu;
	
	//添加
	public String addDepartment() throws IOException, SQLException{
		departmentDao=new DepartmentDaoImpl();
		department.setDept_setup_time(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		departmentDao.save(department);
		return "addDepartment";
	}
	
	public String listDepartment() throws IOException, SQLException{
		departmentDao=new DepartmentDaoImpl();
		if(psu==null){
			psu=new PageSplitUtil(1);
		}
		psu.comput();
		psu.computPageCount(departmentDao.findAllCount());
		departmentList=departmentDao.listDepartment(psu);
		return "listDepartment";
	}
	
	//删除
	public String delDepartment() throws SQLException{
		departmentDao=new DepartmentDaoImpl();
		if(departmentDao.findDepartmentById(department.getDept_id()).getDept_staff_num()<1){
			departmentDao.delDepartment(department.getDept_id());
			return "delDepartment";
		}
		return "fail";
	}
	
	//修改
	public String updateDepartment() throws SQLException{
		departmentDao=new DepartmentDaoImpl();
		departmentDao.updateDepartment(department);
		return "updateDepartment";
	}
	
	public String showUpdateDepartment() throws SQLException{
		departmentDao=new DepartmentDaoImpl();
		department=departmentDao.findDepartmentById(department.getDept_id());
		return "showUpdateDepartment";
	}
	
	
	public List<Department> getDepartmentList() {
		return departmentList;
	}
	public void setDepartmentList(List<Department> departmentList) {
		this.departmentList = departmentList;
	}
	
	public Department getDepartment() {
		return department;
	}
	public void setDepartment(Department department) {
		this.department = department;
	}

	public PageSplitUtil getPsu() {
		return psu;
	}

	public void setPsu(PageSplitUtil psu) {
		this.psu = psu;
	}

}
